class LHProgressBar extends eui.Group implements RES.PromiseTaskReporter
{
    /**进度条颜色 */
    public progressColor: number;
    public progressAlpha: number = 1;
    /**进度条背景颜色 */
    public progressBgColor: number;
    public progressBgAlpha: number = 1;
    /**进度条图片 */
    public progressBarImgName: string;
    /**进度条背景图片 */
    public progressBgImgName: string;
    /**进度条开始和结束处是否显示圆角*/
    public roundBorder:boolean = true;
    /**进度条和进度条背景相交的中间是否显示为圆角*/
    public progressRoundCenter:boolean = false;
    public minValue:number = 0;
    public maxValue:number = 1;
    private value:number = 0;
    /**value：当前进度的百分比小数，total:最大值 */
    public titleFun:(titleLbl:eui.Label,value: number, total: number)=>(void);

    public constructor(x:number=0,y:number=0,width:number=0,height:number=0)
    {
        super();
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }
    private childrenCreatedAlready:boolean = false;
    protected childrenCreated(): void {
        super.childrenCreated();
        this.initData();
        this.childrenCreatedAlready = true;
        this.updateDisplayInfo();
    }
    private initData()
    {
        // 设置默认背景条颜色
        if(!this.progressBgColor && !this.progressBgImgName)
        {
            this.progressBgColor = 0xB7B7B7;
        }
        // 设置默认进度条颜色
        if(!this.progressColor && !this.progressBarImgName)
        {
            this.progressColor = 0x2285F3;
        }
    }
    // 背景条
    private progressBgBar:egret.Shape;
    private progressBar:egret.Shape;
    private progressBgImg:eui.Image;
    private progressBarImg:eui.Image;
    private maskBar:egret.Shape;
    private titleLbl:eui.Label;
    private total:number = 1;
    private updateDisplayInfo()
    {
        // 还没创建过的不做处理
        if(!this.childrenCreatedAlready){
            return;
        }
        // 半径
        var r = this.height/2;
        // 创建背景条
        if(this.progressBgImgName){
            // 去除之前的颜色条
            if(this.progressBgBar){
                this.removeChild(this.progressBgBar);
                this.progressBgBar = null;
            }
            // 没有图片添加图片
            if(!this.progressBgImg){
                this.progressBgImg = new eui.Image();
                this.addChildAt(this.progressBgImg,0);
            }
            this.progressBgImg.source = this.progressBgImgName;
            this.progressBgImg.x = 0;
            this.progressBgImg.y = 0;
            this.progressBgImg.width = this.width;
            this.progressBgImg.height = this.height;
            
        }else if(this.progressBgColor){
            // 是颜色条的去除图片
            if(this.progressBgImg){
                this.removeChild(this.progressBgImg);
                this.progressBgImg = null;
            }
            // 去除之前的颜色条
            if(this.progressBgBar){
                this.removeChild(this.progressBgBar);
                this.progressBgBar = null;
            }
            this.progressBgBar = new egret.Shape();
            this.addChild(this.progressBgBar);
            this.progressBgBar.graphics.clear();
            this.progressBgBar.graphics.beginFill(this.progressBgColor, this.progressBgAlpha);
            //this.progressBgBar.graphics.lineStyle(0, this.progressBgColor);
            this.progressBgBar.graphics.drawRect(0,0,this.width,this.height);
            this.progressBgBar.graphics.endFill();
            this.addChildAt(this.progressBgBar,0);
        }

        var colorWidth = this.width*this.value;
        if(colorWidth>this.width){
            colorWidth = this.width;
        }
        if(colorWidth < 0){
            colorWidth = 0;
        }
        // 创建进度条
        if(this.progressBarImgName){
            // 去除之前的颜色条
            if(this.progressBar){
                this.removeChild(this.progressBar);
                this.progressBar = null;
            }
            // 没有图片添加图片
            if(!this.progressBarImg){
                this.progressBarImg = new eui.Image();
                this.addChild(this.progressBarImg);
            }
            this.progressBarImg.source = this.progressBarImgName;
            this.progressBarImg.x = 0;
            this.progressBarImg.y = 0;
            this.progressBarImg.width = this.width*this.value;
            this.progressBarImg.height = this.height;
        }else if(this.progressColor){
            // 是颜色条的去除图片
            if(this.progressBarImg){
                this.removeChild(this.progressBarImg);
                this.progressBarImg = null;
            }
            // 去除之前的颜色条
            if(this.progressBar){
                this.removeChild(this.progressBar);
                this.progressBar = null;
            }
            
            this.progressBar = new egret.Shape();
            this.addChild(this.progressBar);
            this.progressBar.graphics.clear();
            this.progressBar.graphics.beginFill(this.progressColor, this.progressAlpha);
            if(this.progressRoundCenter){
                // 画圆角
                this.progressBar.graphics.drawArc(r,r,r,Math.PI/2,Math.PI*3/2);
                this.progressBar.graphics.lineTo(colorWidth-r,0);
                this.progressBar.graphics.drawArc(colorWidth-r,r,r,Math.PI*3/2,Math.PI/2);
                this.progressBar.graphics.lineTo(r,this.height);
            }else{
                this.progressBar.graphics.drawRect(0,0,colorWidth,this.height);
            }
            this.progressBar.graphics.endFill();
        }

        if(this.roundBorder){
            if(!this.maskBar){
                this.maskBar = new egret.Shape();
                this.addChild(this.maskBar);
                this.mask = this.maskBar;
            }
            this.maskBar.graphics.clear();
            this.maskBar.graphics.beginFill(0x00ffff, 1);
            this.maskBar.graphics.moveTo(r,r);
            this.maskBar.graphics.drawArc(r, r, r, Math.PI/2, Math.PI*3 / 2);
            this.maskBar.graphics.lineTo(this.width-r, 0);
            this.maskBar.graphics.moveTo(this.width-r,r);
            this.maskBar.graphics.drawArc(this.width-r,r, r, Math.PI*3 / 2,Math.PI/2);
            this.maskBar.graphics.lineTo(r, this.height);
            this.maskBar.graphics.endFill();
           
        }else{
            if(this.maskBar){
                this.maskBar.parent.removeChild(this.maskBar);
                this.mask = null;
                this.maskBar = null;
            }
        }

        // 展示标题
        if(this.titleFun){
            // 把之前的内容删除重新添加
            if(this.titleLbl){
                this.titleLbl.parent.removeChild(this.titleLbl);
                this.titleLbl = null;
            }
            this.titleLbl = new eui.Label();
            this.titleLbl.width = this.width;
            this.titleLbl.height = this.height;
            this.titleLbl.textAlign = egret.HorizontalAlign.CENTER;
            this.titleLbl.verticalAlign = egret.VerticalAlign.MIDDLE;
            this.titleLbl.size = 20;
            this.titleLbl.textColor = 0xffffff;
            this.addChild(this.titleLbl);
            this.titleFun(this.titleLbl,this.value,this.total);
        }else{
            if(this.titleLbl){
                this.titleLbl.parent.removeChild(this.titleLbl);
                this.titleLbl = null;
            }
        }
    }
    public updateShow()
    {
        this.updateDisplayInfo();
    }
    public getValue():number
    {
        return this.value;
    }
    public setValue(value:number, total: number=1)
    {
        this.total = total;
        var curVal = value/total;
        if(this.value != curVal){
            this.value = curVal;
            this.updateShow();
        }
    }
    /**
     * 进度回调
     */
    onProgress(current: number, total: number=1)
    {
        var curVal = current/total;
        this.total = total;
        if(this.value != curVal){
            this.value = curVal;
            this.updateShow();
        }
        
    }
    /**
     * 取消回调
     */
    onCancel()
    {

    }
}